package com.dg.service.impl;

import com.dg.common.PageQuery;
import com.dg.common.PageResult;
import com.dg.dao.AclMapper;
import com.dg.dao.AclModuleMapper;
import com.dg.dto.AclDto;
import com.dg.exception.ParamException;
import com.dg.param.AclParam;
import com.dg.pojo.Acl;
import com.dg.pojo.AclModule;
import com.dg.service.AclService;
import com.dg.util.AclCodeUtil;
import com.dg.util.ValidatorUtil;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Service
public class AclServiceImpl implements AclService {

    @Autowired
    private AclMapper aclMapper;
    @Autowired
    private AclModuleMapper aclModuleMapper;

    @Override
    public PageResult<AclDto> list(Integer aclModuleId, PageQuery pageQuery) {
        ValidatorUtil.validate(pageQuery);
        int count = aclMapper.countByModuleId(aclModuleId);
        List<Acl> list = aclMapper.list(aclModuleId, pageQuery);
        List<AclDto> aclDtoList = Lists.newArrayList();
        for(Acl acl : list){
            AclModule aclModule = aclModuleMapper.selectByPrimaryKey(acl.getAclModuleId());
            AclDto aclDto = AclDto.adapter(acl, aclModule);
            aclDtoList.add(aclDto);
        }

        Collections.sort(aclDtoList, new Comparator<AclDto>() {
            @Override
            public int compare(AclDto o1, AclDto o2) {
                return o1.getSeq()-o2.getSeq();
            }
        });

        return PageResult.<AclDto>builder().total(count).data(aclDtoList).build();
    }

    @Override
    public void save(AclParam param) {
        ValidatorUtil.validate(param);
        if(checkExist(param.getId(), param.getAclModuleId(), param.getName())){
            throw new ParamException("权限点名称已存在");
        }
        Acl acl = Acl.builder().name(param.getName()).aclModuleId(param.getAclModuleId()).type(param.getType())
                .seq(param.getSeq()).url(param.getUrl()).status(param.getStatus()).remark(param.getRemark())
                .operator("admin").operateIp("127.0.0.1").operateTime(new Date()).build();
        acl.setCode(AclCodeUtil.generatorAclCode());

        aclMapper.insertSelective(acl);
    }

    @Override
    public void update(AclParam param){
        ValidatorUtil.validate(param);
        if(checkExist(param.getId(), param.getAclModuleId(), param.getName())){
            throw new ParamException("权限点名称已存在");
        }
        Acl acl = Acl.builder().id(param.getId()).name(param.getName()).aclModuleId(param.getAclModuleId()).type(param.getType())
                .seq(param.getSeq()).url(param.getUrl()).status(param.getStatus()).remark(param.getRemark())
                .operator("admin").operateIp("127.0.0.1").operateTime(new Date()).build();
        acl.setCode(AclCodeUtil.generatorAclCode());

        aclMapper.updateByPrimaryKeySelective(acl);
    }

    @Override
    public void saveOrUpdate(AclParam param) {
        ValidatorUtil.validate(param);
        if(checkExist(param.getId(), param.getAclModuleId(), param.getName())){
            throw new ParamException("权限点名称已存在");
        }
        Acl acl = Acl.builder().name(param.getName()).aclModuleId(param.getAclModuleId()).type(param.getType())
                .seq(param.getSeq()).url(param.getUrl()).status(param.getStatus()).remark(param.getRemark())
                .operator("admin").operateIp("127.0.0.1").operateTime(new Date()).build();

        if (param.getId() == null){
            acl.setCode(AclCodeUtil.generatorAclCode());
            aclMapper.insertSelective(acl);
        }else{
            aclMapper.updateByPrimaryKeySelective(acl);
        }

    }

    @Override
    public void delete(Integer id) {
        if(id == null){
            throw new ParamException("id不能为空");
        }
        aclMapper.deleteByPrimaryKey(id);
    }




    private boolean checkExist(Integer id, Integer aclModuleId, String name) {
        return aclMapper.findByName(id, aclModuleId, name) > 0;
    }

}
